package pw.archipelag.loaderGame.panel.scores {
	import base.uiComponents.ScrollAreaWithGradientMask;
	import base.types.ArrayExt;
	import flash.display.Sprite;
	import pw.archipelag.loaderGame.Config;
	import flash.events.MouseEvent;
	import pw.archipelag.loaderGame.interfaceGame.skinsPanel.SkinScores;
	import flash.display.DisplayObject;
	
	public class ScrollAreaScores extends ScrollAreaWithGradientMask {
		
		internal var arrObjResult: ArrayExt;
		private var heightLastItem: Number;
		
		public function ScrollAreaScores(arrObjResult: ArrayExt, heightCover: Number = 4): void {
			this.arrObjResult = arrObjResult;
			super(0, heightCover);
		}
		
		override protected function createArea(): Sprite {
			var area: Sprite = new Sprite();
			var indScoreCurrentLogin: int = this.arrObjResult.getElementIndexByProperty("login", Config.DESCRIPTION_PLAYER.login);
			for (var i: uint = 0; i < this.arrObjResult.length; i++) {
				if (indScoreCurrentLogin == i) {
					if ((indScoreCurrentLogin == this.arrObjResult.length - 1) && (this.arrObjResult.length > 1) && (this.arrObjResult[indScoreCurrentLogin].lp > this.arrObjResult[indScoreCurrentLogin - 1].lp + 1)) {
						var itemScoreLine: ItemScoreLine = new ItemScoreLine(i % 2);
						area.addChild(itemScoreLine);
						this.arrObjResult.splice(i++, 0, {item: itemScoreLine, login: ""});
					}
				}
				var itemScore: ItemScore = new ItemScore(this.arrObjResult[i], i % 2);
				//itemScore.addEventListener(MouseEvent.MOUSE_DOWN, this.onItemScoreClick);
				this.arrObjResult[i].item = itemScore;
				area.addChild(itemScore);
			}
			return area;
		}
		
		protected function onItemScoreClick(e: MouseEvent): void {
			var selectedItemScore: ItemScore = ItemScore(e.currentTarget);
		}
		
		override protected function getTargetPosYForPercent(percent: Number): Number {
			return Math.min(0, Math.max(this.height - this.area.height, percent * (this.height - this.heightLastItem - this.area.height)));
		}
		
		internal function draw(skinScores: SkinScores): void {
			var indScoreCurrentLogin: int = this.arrObjResult.getElementIndexByProperty("login", Config.DESCRIPTION_PLAYER.login);
			for (var i: uint = 0; i < this.arrObjResult.length; i++) {
				var itemScore: ItemScoreAbstract = this.arrObjResult[i].item;
				itemScore.draw(skinScores);
				if (indScoreCurrentLogin == i)
					ItemScore(itemScore).changeColorTfsCurrentLoginToHighlighted(skinScores.colorTFHighlight);
			}
		}
		
		override public function set width(value: Number): void {
			this.maskArea.width = value;
			for (var i: uint = 0; i < this.arrObjResult.length; i++)
				DisplayObject(this.arrObjResult[i].item).width = value;
		}
		
		override public function set height(value: Number): void {
			if ((this.arrObjResult.length > 0) && (value > 0)) {
				var itemScore: DisplayObject;
				for (var i: uint = 0; i < this.arrObjResult.length; i++) {
					itemScore = this.arrObjResult[i].item;
					itemScore.y = i * itemScore.height;
				}
				ScrollScores.COUNT_ITEMS_VISIBLE = Math.ceil(value / itemScore.height);
				this.heightLastItem = value % itemScore.height;
			}
			super.height = value;
		}
		
		private function removeArea(): void {
			for (var i: uint = 0; i < this.arrObjResult.length; i++) {
				var itemScore: DisplayObject = this.arrObjResult[i].item;
				itemScore.removeEventListener(MouseEvent.MOUSE_DOWN, this.onItemScoreClick);
				this.area.removeChild(itemScore);
				this.arrObjResult[i].item = null;
			}
		}
		
		override public function destroy(): void {
			this.removeArea();
			super.destroy();
		}
		
	}

}